package org.gdstash.file;

import org.gdstash.db.DBFormulaSet;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ARZFormulaSetPool {
    public static final String TEMPLATE_COSTFORMULA = "database/templates/itemcost.tpl";
    private static List<ARZRecord> records;

    public static boolean isFormula(final String template) {
        return template != null && template.equals("database/templates/itemcost.tpl");
    }

    public static void storeFormulaSet(final ARZRecord record) {
        final String fn = record.getFileName();
        final String tpl = record.getTemplate();
        if (fn == null) {
            return;
        }
        if (tpl == null) {
            return;
        }
        if (!isFormula(tpl)) {
            return;
        }
        final Iterator<ARZRecord> iter = ARZFormulaSetPool.records.iterator();
        while (iter.hasNext()) {
            final ARZRecord rec = iter.next();
            if (rec.getFileName().equals(fn)) {
                iter.remove();
                break;
            }
        }
        ARZFormulaSetPool.records.add(record);
    }

    public static DBFormulaSet getFormulaSet(final String formulaSetID) {
        DBFormulaSet set = null;
        for (final ARZRecord rec : ARZFormulaSetPool.records) {
            if (rec.getFileName().equals(formulaSetID)) {
                set = rec.getDBFormulaSet();
                break;
            }
        }
        return set;
    }

    static {
        ARZFormulaSetPool.records = new LinkedList<ARZRecord>();
    }
}
